angular.module('planList', ['ui.grid', 'ui.grid.pagination',
        'ui.grid.resizeColumns', 'ui.grid.moveColumns', 'toastr', 'common'])
    .controller('planListController',
        ['$scope', '$http', 'toastr', 'commonService',
            function ($scope, $http, toastr) {
                $scope.filter = {
                    start_date: {from: null, to: null},
                    end_date: {from: null, to: null},
                    page: 1,
                    size: 10
                };

                if (!!$.cookie("toast")) {
                    toastr.success($.cookie("toast"));
                    $.cookie("toast", "");
                }

                $scope.data = [];

                $scope.columnOptions = [
                    {
                        field: "pl_name",
                        name: "计划名称",
                        enableColumnMenu: false,
                        cellTemplate: '<span class="grid-data">{{MODEL_COL_FIELD}}</span>'
                    },
                    {
                        field: "start_date",
                        name: "开始时间",
                        enableColumnMenu: false,
                        cellTemplate: '<span class="grid-data">{{MODEL_COL_FIELD}}</span>'
                    },
                    {
                        field: "end_date",
                        name: "结束时间",
                        enableColumnMenu: false,
                        cellTemplate: '<span class="grid-data">{{MODEL_COL_FIELD}}</span>'
                    },
                    {
                        field: "next_plan",
                        name: "后续安排",
                        enableColumnMenu: false,
                        cellTemplate: '<span class="grid-data">{{MODEL_COL_FIELD}}</span>'
                    },
                    {
                        field: "pl_id",
                        name: "查看",
                        width: 50,
                        enableColumnMenu: false,
                        cellTemplate: '<a class="grid-option fa fa-calendar" href="/plan/find?plan_id={{MODEL_COL_FIELD}}"></a>'
                    },
                    {
                        field: "pl_id",
                        name: "导出",
                        width: 50,
                        enableColumnMenu: false,
                        cellTemplate: '<a class="grid-option fa fa-file-excel-o" target="_blank" href="/plan/export/{{MODEL_COL_FIELD}}"></a>'
                    }
                ];

                $scope.gridOption = {
                    enableVerticalScrollbar: 0,
                    enableColumnResizing: true,
                    enableSelectAll: true,
                    exporterOlderExcelCompatibility: true,
                    enablePaging: true,
                    paginationPageSizes: [10, 25, 50],
                    paginationPageSize: 10,
                    useExternalPagination: true,
                    paginationTemplate: '/views/gridPagination.html',
                    enableGridMenu: false,
                    enableRowSelection: true,
                    excessRows: 1000,
                    columnDefs: $scope.columnOptions,
                    data: $scope.data
                };

                $scope.gridOption.onRegisterApi = function (gridApi) {
                    $scope.gridApi = gridApi;
                    gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                        $scope.filter.page = newPage;
                        $scope.filter.size = pageSize;
                        $scope.gridOption.paginationPageSize = pageSize;
                        queryData();
                    });
                };

                $scope.reset = function () {
                    $scope.filter = {
                        start_date: {from: null, to: null},
                        end_date: {from: null, to: null},
                        page: 1,
                        size: 10
                    };
                    $(".date").each(function(){
                        $(this).val("");
                        $(this).change();
                    });
                    queryData();
                };

                $scope.search = function () {
                    queryData();
                };

                function queryData() {
                    $scope.filter.limit = ($scope.filter.page - 1) * $scope.filter.size;
                    $scope.filter.count = $scope.filter.limit + $scope.filter.size;
                    $http.post('/plan/findList', $scope.filter).success(function (data) {
                        data.data.forEach(function (e) {
                            e.start_date = new Date(e.start_date).Format('yyyy-MM-dd');
                            e.end_date = new Date(e.end_date).Format('yyyy-MM-dd');
                        });
                        $scope.gridOption.data = data.data;
                        $scope.gridOption.totalItems = data.total;
                        setTimeout(function(){
                            $(window).resize();
                        },200);
                        if (data.data.length == 0) {
                            toastr.success("未查询到数据!");
                        }
                    }).error(function () {
                        toastr.error("请检查网络连接");
                    });
                }

                $(".date").each(function () {
                    var self = this;
                    $(this).datetimepicker({
                        format: 'Y/m/d',
                        timepicker: false,
                        onClose: function () {
                            var temp = $(self).attr("id").split("-");
                            $scope.filter[temp[0]][temp[1]] = $(self).val();
                        }
                    });
                });

                setTimeout(function(){
                    queryData();
                },100);
            }
        ]);